<?php

namespace App\Repositories;

use App\Models\AccountRecord;
use App\Models\Member;
use App\Repositories\Api\MemberRepository;
use Exception;
use Illuminate\Support\Facades\DB;

class AccountRecordRepository
{
    public function getPages(array $filter = [], int $pageSize = 10, int $pageNum = 1)
    {
        $res = AccountRecord::query()
            ->where("uid", "=", $filter["wechat_admin_id"])
            ->where("is_delete", "=", 1)
            ->where("create_time", ">", date("Y-m-d H:i:s", strtotime("-1 months")))
            ->when(intval($filter['type']), function ($query) use ($filter) {
                return $query->where("inout", "=", intval($filter['type']) - 1);
            })
            ->orderBy('id', 'desc')
            ->paginate($filter['limit'] ? $filter['limit'] : $pageSize, "*", null, $pageNum)
            ->toArray();

        $res = filterPaginateData($res);
        foreach ($res['items'] as $k => $v) {
            // $res['items'][$k]['summary'] = strip_tags($v['content_detail']);
            $res['items'][$k]['type_name'] = AccountRecord::account_record_type($v['type']);
        }

        return $res;
    }

    public function getOne($id = NULL)
    {
        if (!$id) return NULL;
        $res = AccountRecord::where("id", "=", $id)->first();
        $res['type_name'] = AccountRecord::account_record_type($res['type']);
        if (!in_array($res['type'], [3, 4, 16, 17, 18, 19, 52, 57, 63, 64, 69])) {// 抽取微信二维码 不显示 对方昵称
            $res['df_uid'] && $res['df_username'] = Member::where("id", "=", $res['df_uid'])->value("username");
        }

        return $res;
    }

    //增加访问奖励
    public function addAwardEveryday($type = 0, $award = 0.0, $df_uid = null)
    {
        if (empty($type) || !in_array($type, [63, 64]) || !$award) return false;

        DB::beginTransaction();

        try {
            //互选池限制每天最多奖励2限时联谊币
            $visit_award_total = 0;
            if ($type == 64) {
                $visit_award_total = AccountRecord::where('create_time', "like", date("Y-m-d") . "%")
                    ->where('inout', 1)
                    ->where(['uid' => UID])
                    ->where("type", 64)
                    ->groupBy("df_uid")
                    ->pluck("amount")
                    ->toArray();

                $visit_award_total = array_sum($visit_award_total);
                if ($visit_award_total == 0) {
                    $award = 1;// 每天首次访问，奖励1联谊币
                }
                //访问互选池奖励
                if ($visit_award_total >= C('visit_electpool_price'))
                    return false;
            }

            //奖励过不在奖励
            if (AccountRecord::where(['type' => $type, 'uid' => UID, 'df_uid' => $df_uid ?? null])->where('create_time', "like", date("Y-m-d") . "%")->count("id")) {
                return false;
            }

            (new MemberRepository())->change_user_account(UID, $df_uid ?? null, null, $type, $award, null, 1, nextMonday());
            //提交事务
            DB::commit();
            return $award;
        } catch (Exception $e) {
            //还原（回滚)事务
            DB::rollBack();
            return false;
        }
    }

}

